<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/14
 * Time: 13:55
 */
namespace app\modules\setting\controllers\auth;


use app\common\enum\Adminuser_typeEnum;
use app\models\EtcOfficeVideoUpload;
use app\modules\setting\configs\auth\OfficeConfig;
use app\modules\setting\models\seller\EtcOfficeProductSelectedSearch;
use app\modules\setting\models\seller\EtcSellerTiktokProduct;
use sookie\auth\controllers\AdminuserController;
use sookie\auth\models\AdminuserSearch;
use yii\helpers\ArrayHelper;
use Yii;

class Office_goodsController extends AdminuserController
{
    /**
     * @return array
     * 列表数据
     */
    public function actionIndex()
    {
        try {
            $searchModel = new AdminuserSearch();
            $params = Yii::$app->request->queryParams;
            $page = ArrayHelper::getValue($params,'page',1);
            $pageSize = ArrayHelper::getValue($params,'pageSize',10);

            unset($params['r'],$params['page'],$params['pageSize']);
            $params['type'] = Adminuser_typeEnum::OFFICE;
            $dataProvider = $searchModel->search(["AdminuserSearch" => $params,'page' => $page],$pageSize);
            $data = $dataProvider->getModels();
            $data = array_map(function($item){
                $re=[];
                if(is_object($item)){
                    $re= $item->toArray();
                }
                $selected_num = EtcSellerTiktokProduct::find()->andWhere(['user_id'=>$item['id']])->count("*");
                $selected_link = "/admin/build/#/generate/ordinary/admin.setting.auth.dhlb.index?user_id={$item['id']}";
                $selected_product_num = EtcSellerTiktokProduct::find()->andWhere(['user_id'=>$item['id']])->count("distinct goods_id");
                $selected_product_link = "/admin/build/#/generate/ordinary/admin.setting.auth.dhlb.index?user_id={$item['id']}";
                $video_num = EtcOfficeVideoUpload::find()->andWhere(['user_id'=>$item['id']])->count("*");
                $video_link = "/admin/build/#/generate/ordinary/admin.setting.auth.pssplb.index?user_id={$item['id']}";
                $tiktok_num = EtcOfficeVideoUpload::find()->andWhere(['user_id'=>$item['id']])->count("distinct tiktok_id");
                $tiktok_link = "/admin/build/#/generate/ordinary/admin.setting.auth.pssplb.index?user_id={$item['id']}";
                $re['selected_num'] = "<a href='{$selected_link}' target='_blank'>{$selected_num}</a>";
                $re['selected_product_num'] = "<a href='{$selected_product_link}' target='_blank'>{$selected_product_num}</a>";
                $re['video_num'] = "<a href='{$video_link}' target='_blank'>{$video_num}</a>";
                $re['tiktok_num'] = "<a href='{$tiktok_link}' target='_blank'>{$tiktok_num}</a>";
                return $re;
            },$data);
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],OfficeConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }
}